package com.jt.service;

import com.jt.pojo.User;
import com.jt.utils.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@Service
public class WebServiceImpl implements WebUserService {

    @Override
    public List<User> findAll() {
        HttpClient client = HttpClients.createDefault();
        String url = "http://sso.jt.com/user/testHttpClient";
        HttpGet get = new HttpGet(url);
        List<User> list = new ArrayList<>();
        try {
            HttpResponse response = client.execute(get);
            int status = response.getStatusLine().getStatusCode();
            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8");
                list = ObjectMapperUtil.toObject(result, list.getClass());
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        return list;
    }
}
